#include "qgraphicdialog.h"
#include "ui_qgraphicdialog.h"
#include <QPainter>
#include <QPoint>
#include <qpaintutils.h>
#include <QColorDialog>

QGraphicDialog::QGraphicDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QGraphicDialog)
{
    ui->setupUi(this);

    textValidationFailed = false;
    diameterValidationFailed = false;
    colorValidationFailed = true;

    textLabel = new QLabel("Text: ");
    textEdit = new QLineEdit;
    textLabel->setBuddy(textEdit);

    diameterLabel = new QLabel("Diameter: ");
    diameterEdit = new QLineEdit;
    diameterLabel->setBuddy(diameterEdit);

    selectColorButton = new QPushButton("Select color");

    buttonBox = new QDialogButtonBox;
    buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(okEnabled());

    mainLayout = new QGridLayout;

    mainLayout->setColumnStretch(0, 1);
    mainLayout->setColumnStretch(3, 1);

    mainLayout->addWidget(textLabel, 1, 0, Qt::AlignCenter);
    mainLayout->addWidget(textEdit, 1, 1);
    mainLayout->addWidget(diameterLabel, 2, 0, Qt::AlignCenter);
    mainLayout->addWidget(diameterEdit, 2, 1);
    mainLayout->addWidget(selectColorButton, 3, 0, 1, 4);
    mainLayout->addWidget(buttonBox, 4, 0, 1, 2);

    connect(selectColorButton, &QPushButton::clicked, this, &QGraphicDialog::onSelectColor);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QGraphicDialog::onOk);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QGraphicDialog::close);
    connect(textEdit, &QLineEdit::editingFinished, this, &QGraphicDialog::validateText);
    connect(diameterEdit, &QLineEdit::editingFinished, this, &QGraphicDialog::validateDiameter);

    setLayout(mainLayout);
    setWindowTitle("Set drawing params");
}

QGraphicDialog::~QGraphicDialog()
{
    delete buttonBox;
    delete selectColorButton;
    delete diameterEdit;
    delete diameterLabel;
    delete textEdit;
    delete textLabel;
    delete mainLayout;
    delete ui;
}

void QGraphicDialog::onSelectColor()
{
    QColor color = QColorDialog::getColor(Qt::black, this);
    if(color.isValid())
    {
        this->color = new QColor(color);
        colorValidationFailed = false;
    }
    else
    {
        colorValidationFailed = true;
    }
    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(okEnabled());
}

void QGraphicDialog::onOk()
{
    emit closed(color, textEdit->text(), diameterEdit->text().toInt());
    this->close();
}

bool QGraphicDialog::okEnabled()
{
    return !textValidationFailed && !diameterValidationFailed && !colorValidationFailed;
}

void QGraphicDialog::validateColor()
{
    if(color->isValid())
    {
        colorValidationFailed = false;
        buttonBox->button(QDialogButtonBox::Ok)->setEnabled(okEnabled());
    }
}

void QGraphicDialog::validateDiameter()
{
    if (diameterEdit->text().isEmpty())
    {
        diameterEdit->setStyleSheet("border: 1px solid red");
        diameterValidationFailed = true;
    }
    else if (!QRegExp("\\d*").exactMatch(diameterEdit->text()))
    {
        diameterEdit->setStyleSheet("border: 1px solid red");
        diameterValidationFailed = true;
    }
    else
    {
        diameterEdit->setStyleSheet("border: 1px solid");
        diameterValidationFailed = false;
    }
    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(okEnabled());
}

void QGraphicDialog::validateText()
{
    if (textEdit->text().isEmpty())
    {
        textEdit->setStyleSheet("border: 1px solid red");
        textValidationFailed = true;
    }
    else
    {
        textEdit->setStyleSheet("border: 1px solid");
        textValidationFailed = false;
    }
    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(okEnabled());
}
